package ATM;

import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

public class atm {
    private ArrayList<Account> user = new ArrayList<>();
    private Scanner sc = new Scanner(System.in);
    Random r = new Random();
    public void startUp(){
        while (true) {
            System.out.println("=========欢迎使用无名银行系统=========");
            System.out.println("1.开通账户");
            System.out.println("2.登录账号");
            System.out.println("请你选择以上操作：");
            int num = sc.nextInt();
            switch (num){
                case 1 :
                    //开通账户
                    openAccount();
                    break;
                case 2 :
                    //登录账号
                    loginAccount();
                    break;
                default:
                    System.out.println("没有该操作，请重新选择");
            }
        }
    }

    private void openAccount(){
        Account account = new Account();
        System.out.println("====注册账户====");
        //1.输入姓名
        System.out.println("请您输入姓名：");
        String name  = sc.next();
        account.setName(name);
        //2.输入性别
        while (true) {
        System.out.println("请您输入性别：");
        char genderInput = sc.next().charAt(0);
            if(genderInput == '男' || genderInput == '女'){
                account.setGender(genderInput);
                break;
            }else {
                System.out.println("请输入有效性别");
            }
        }
        //3.设置密码
        while (true) {
            System.out.println("请您输入密码：");
            String passworld = sc.next();
            System.out.println("请您再次确认密码：");
            String confirmpassworld = sc.next();
            if(confirmpassworld.equals(passworld)){
                //密码设置成功
                System.out.println("密码设置成功！");
                account.setPassworld(passworld);
                break;
            }else{
                System.out.println("两次密码不一样，请重新设置！");
            }
        }
        //4.设置限额
        System.out.println("请您输入取现额度");
        double limit = sc.nextDouble();
        account.setLimit(limit);

        //4.生成一个八位数账号
        String cardId = centerCardId();
        account.setCardID(cardId);
        user.add(account);
        System.out.println("恭喜您，" + account.getName() + "账户注册成功，你的卡号是： " + cardId);
    }

    private void loginAccount(){
        //登录页面
        System.out.println("====账号登录====");
        if (user.size() == 0){
            System.out.println("当前系统中为账户，请先注册");
            return;
        }
        while (true) {
            System.out.println("请输入您的卡号：");
            String userCardId = sc.next();
            Account account = getAccountByCardId(userCardId);
            if (account == null) {
                System.out.println("您输入的卡号不存在，请重新确认");
            }else {
                while (true) {
                    System.out.println("请输入您的密码：");
                    String passworld = sc.next();
                    if (account.getPassworld().equals(passworld)){
                        System.out.println("恭喜您" + account.getName() + "成功登陆系统，您的卡号是： " + account.getCardID());
                        businessPage(account);
                        return;
                    }else {
                        System.out.println("您输入的密码密码有误，请重新输入：");
                    }
                }
            }
        }
    }

    //业务页面
    private void businessPage(Account account){
        while (true) {
            System.out.println("====您可以办理一下业务====");
            System.out.println("1.账户查询");
            System.out.println("2.存款");
            System.out.println("3.取款");
            System.out.println("4.转账");
            System.out.println("5.修改密码");
            System.out.println("6.退出");
            System.out.println("7.注销账户");
            int num = sc.nextInt();
            switch (num){
                case 1 :
                    //账户查询
                    checkAccount(account);
                    break;
                case 2 :
                    //存款
                    deposit(account);
                    break;
                case 3 :
                    //取款
                    drawMoney(account);
                    break;
                case 4 :
                    //转账
                    remit(account);
                    break;
                case 5 :
                    //修改密码
                    changepassworld(account);
                    break;
                case 6 :
                    //退出
                    System.out.println("退出系统成功");
                    return;
                case 7 :
                    //注销账户
                    if(cancelAccount(account)){
                        //销户成功
                        return;
                    }
                    break;
                default:
                    System.out.println("请选择有效业务");
            }
        }
    }
    //查询账户
    private void checkAccount( Account account){
        System.out.println("===当前您的账户信息===");
        System.out.println("姓名： " + account.getName());
        System.out.println("性别： " + account.getGender());
        System.out.println("卡号： " + account.getCardID());
        System.out.println("密码： " + account.getPassworld());
        System.out.println("余额： " + account.getBalance());
        System.out.println("限额： " + account.getLimit());
    }

    //存款
    private void deposit(Account account){
        System.out.println("====存钱====");
        System.out.println("请输入您的存款金额： ");
        Double money = sc.nextDouble();
        account.setBalance(account.getBalance() + money);
        System.out.println("您当前的账户余额为： " + account.getBalance());
    }

    //取钱
    private void drawMoney(Account account){
        System.out.println("====取钱====");
        if(account.getBalance() < 100){
            System.out.println("您账户的余额不足一百元，请转往柜台办理此项业务");
            return;
        }
        while (true) {
            System.out.println("请输入您的取款金额： ");
            Double money = sc.nextDouble();
            if (money <= account.getBalance()){
                //钱够，能取，判断超没超限额
                if(money > account.getLimit()){
                    System.out.println("抱歉，您取款的金额超过了限额");
                    System.out.println("你的限额为： " + account.getLimit());
                }else {
                    account.setBalance(account.getBalance() - money);
                    System.out.println("您取款" + money + "元成功");
                    System.out.println("当前账户余额为： " + account.getBalance());
                    break;
                }
            }else {
                System.out.println("抱歉，您的余额不足");
                System.out.println("您的余额为： " + account.getBalance());
            }
        }

    }

    //修改密码
    private void changepassworld(Account account){
        System.out.println("====修改密码====");
        while (true) {
            System.out.println("请输入当前账户密码： ");
            String oldpassworld = sc.next();
            if(account.getPassworld().equals(oldpassworld)){
                while (true) {
                    System.out.println("请您输入新的密码: ");
                    String newpassworld = sc.next();
                    System.out.println("请再确认密码（必须与新密码一致）：");
                    String okNewPassworld = sc.next();
                    if(newpassworld.equals(okNewPassworld)){
                        //两次认证一样
                        account.setPassworld(okNewPassworld);
                        System.out.println("恭喜您密码修改成功！");
                        return;
                    }else{
                        //两次认证不一样
                        System.out.println("两次输入的密码不同，请重新输入");
                    }
                }
            }else {
                System.out.println("您输入的密码有误，请重新输入");
            }
        }

    }

    //转账
    private void remit(Account account){
        if(user.size() < 2){
            System.out.println("抱歉，当前系统中只有您一个用户");
            return;
        }

        if(account.getBalance() == 0){
            System.out.println("抱歉，您当前的账户余额为0.0元，无法进行转账");
            return;
        }
        while (true) {
            System.out.println("请输入对方的账号 ： ");
            String cardId = sc.next();
            Account acc = getAccountByCardId(cardId);
            //验证账号是否存在
            if(acc == null){
                System.out.println("抱歉，您想转账的用户不存在，请重新确认对方账号");
            }else {
                while (true) {
                    //验证对方姓名
                    String name = acc.getName().substring(1);
                    System.out.println("请输入【*" + name + "】的姓氏进行验证：");
                    String familyName = sc.next();
                    if(acc.getName().startsWith(familyName)){
                        while (true) {
                            //进行转账
                            System.out.println("请输入您的转账金额：");
                            Double money = sc.nextDouble();
                            if (money > account.getBalance()){   //钱不够
                                System.out.println("抱歉，您的账户余额不足，无法进行转账");
                                System.out.println("您最多能转" + account.getBalance());
                            }else {
                                account.setBalance(account.getBalance() - money); //更新自己的账户余额
                                acc.setBalance(acc.getBalance() + money);  //更新对方的账户余额
                                return;
                            }
                        }
                    }else{
                        System.out.println("验证错误！");
                    }
                }
            }
        }
    }

    //注销账户
    private boolean cancelAccount(Account account){
        System.out.println("====注销账户====");
        while (true) {
            System.out.println("您确定要注销账户吗?");
            System.out.println("1.确定");
            System.out.println("2.退出");
            int num = sc.nextInt();
            switch (num){
                case 1 :
                    //确认注销账户
                    if(account.getBalance() > 0){
                        System.out.println("您的帐户里还有余额，请取出后在注销");
                        break;
                    }else{
                        user.remove(account);
                        System.out.println("注销成功");
                        return true;
                    }
                case 2 :
                    //退出操作，结束方法
                    return false;
                default:
                    System.out.println("请输入有效操作");
            }
        }
    }

    //生成八位数卡号
    private String centerCardId(){
        while (true) {
            String cardId = "";
            for (int i = 0; i < 8; i++) {
                int id = r.nextInt(10);
                cardId += id;
            }
            if(getAccountByCardId(cardId) == null){
                return cardId;
            }

        }
    }

    //校验卡号是否重复
    private Account getAccountByCardId(String cardId){
        for (int i = 0; i < user.size(); i++) {
            Account account = user.get(i);
            if(account.getCardID().equals(cardId)) {
                return account;
            }
        }
        return null;
    }
}

